<?php
/**
 * 事务列表页面
 */
require_once '../../includes/header.php';

// 检查用户是否已登录
if (!isLoggedIn()) {
    header('Location: /login.php');
    exit;
}

// 获取状态过滤
$status = isset($_GET['status']) ? sanitizeInput($_GET['status']) : 'all';
$validStatuses = ['all', 'pending', 'completed', 'expired'];
if (!in_array($status, $validStatuses)) {
    $status = 'all';
}

// 获取用户的事务列表
$task = new Task();
$tasks = $task->getUserTasks($_SESSION['user_id'], $status);

// 更新过期事务
$task->updateExpiredTasks();

// 获取通知消息
$success_message = $_SESSION['success_message'] ?? null;
unset($_SESSION['success_message']);
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
                    <h4 class="mb-0">我的事务</h4>
                    <a href="/views/task/create.php" class="btn btn-light btn-sm">
                        <i class="fas fa-plus"></i> 新建事务
                    </a>
                </div>
                <div class="card-body">
                    <?php if (isset($success_message)): ?>
                        <div class="alert alert-success"><?php echo $success_message; ?></div>
                    <?php endif; ?>
                    
                    <!-- 状态过滤 -->
                    <div class="mb-3">
                        <div class="btn-group" role="group">
                            <a href="?status=all" class="btn btn-<?php echo $status == 'all' ? 'primary' : 'outline-primary'; ?>">全部</a>
                            <a href="?status=pending" class="btn btn-<?php echo $status == 'pending' ? 'primary' : 'outline-primary'; ?>">待完成</a>
                            <a href="?status=completed" class="btn btn-<?php echo $status == 'completed' ? 'primary' : 'outline-primary'; ?>">已完成</a>
                            <a href="?status=expired" class="btn btn-<?php echo $status == 'expired' ? 'primary' : 'outline-primary'; ?>">已过期</a>
                        </div>
                    </div>
                    
                    <?php if (count($tasks) > 0): ?>
                        <div class="list-group">
                            <?php foreach ($tasks as $task): ?>
                                <?php
                                // 获取状态样式
                                $statusClass = '';
                                $statusText = '';
                                switch ($task['status']) {
                                    case 'pending':
                                        $statusClass = 'bg-warning';
                                        $statusText = '待完成';
                                        break;
                                    case 'completed':
                                        $statusClass = 'bg-success';
                                        $statusText = '已完成';
                                        break;
                                    case 'expired':
                                        $statusClass = 'bg-danger';
                                        $statusText = '已过期';
                                        break;
                                }
                                
                                // 计算剩余时间
                                $deadlineTime = strtotime($task['deadline']);
                                $currentTime = time();
                                $timeRemaining = '';
                                
                                if ($task['status'] == 'pending') {
                                    $remainingSeconds = $deadlineTime - $currentTime;
                                    if ($remainingSeconds > 0) {
                                        $days = floor($remainingSeconds / 86400);
                                        $hours = floor(($remainingSeconds % 86400) / 3600);
                                        
                                        if ($days > 0) {
                                            $timeRemaining = "剩余 {$days} 天 {$hours} 小时";
                                        } else {
                                            $minutes = floor(($remainingSeconds % 3600) / 60);
                                            $timeRemaining = "剩余 {$hours} 小时 {$minutes} 分钟";
                                        }
                                    } else {
                                        $timeRemaining = "已过期";
                                    }
                                }
                                ?>
                                <a href="/views/task/detail.php?id=<?php echo $task['id']; ?>" class="list-group-item list-group-item-action">
                                    <div class="d-flex w-100 justify-content-between">
                                        <h5 class="mb-1"><?php echo htmlspecialchars($task['title']); ?></h5>
                                        <span class="badge <?php echo $statusClass; ?>"><?php echo $statusText; ?></span>
                                    </div>
                                    <p class="mb-1"><?php echo nl2br(htmlspecialchars($task['description'])); ?></p>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <small class="text-muted">
                                            截止时间: <?php echo date('Y-m-d H:i', strtotime($task['deadline'])); ?>
                                        </small>
                                        <?php if (!empty($timeRemaining)): ?>
                                            <small class="text-<?php echo $remainingSeconds > 0 ? 'warning' : 'danger'; ?>">
                                                <?php echo $timeRemaining; ?>
                                            </small>
                                        <?php endif; ?>
                                    </div>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-info">
                            <?php if ($status == 'all'): ?>
                                您还没有创建任何事务，<a href="/views/task/create.php">立即创建</a>
                            <?php else: ?>
                                没有<?php echo $status == 'pending' ? '待完成' : ($status == 'completed' ? '已完成' : '已过期'); ?>的事务
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once '../../includes/footer.php'; ?>
